package com.ruoyi.jxxwx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jxxwx.domain.bo.JxxPlaceBo;
import com.ruoyi.jxxwx.domain.vo.JxxPlaceVo;
import com.ruoyi.jxxwx.domain.JxxPlace;
import com.ruoyi.jxxwx.mapper.JxxPlaceMapper;
import com.ruoyi.jxxwx.service.IJxxPlaceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 场所信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-12
 */
@RequiredArgsConstructor
@Service
public class JxxPlaceServiceImpl implements IJxxPlaceService {

    private final JxxPlaceMapper baseMapper;

    /**
     * 查询场所信息
     */
    @Override
    public JxxPlaceVo queryById(Long placeId){
        return baseMapper.selectVoById(placeId);
    }

    /**
     * 查询场所信息列表
     */
    @Override
    public TableDataInfo<JxxPlaceVo> queryPageList(JxxPlaceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JxxPlace> lqw = buildQueryWrapper(bo);
        Page<JxxPlaceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询场所信息列表
     */
    @Override
    public List<JxxPlaceVo> queryList(JxxPlaceBo bo) {
        LambdaQueryWrapper<JxxPlace> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JxxPlace> buildQueryWrapper(JxxPlaceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JxxPlace> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlaceName()), JxxPlace::getPlaceName, bo.getPlaceName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceAddr()), JxxPlace::getPlaceAddr, bo.getPlaceAddr());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceType()), JxxPlace::getPlaceType, bo.getPlaceType());
        lqw.eq(StringUtils.isNotBlank(bo.getPlaceArea()), JxxPlace::getPlaceArea, bo.getPlaceArea());
        lqw.eq(bo.getPlaceStatus() != null, JxxPlace::getPlaceStatus, bo.getPlaceStatus());
        lqw.eq(bo.getCorrectionTime() != null, JxxPlace::getCorrectionTime, bo.getCorrectionTime());
        return lqw;
    }

    /**
     * 新增场所信息
     */
    @Override
    public Boolean insertByBo(JxxPlaceBo bo) {
        JxxPlace add = BeanUtil.toBean(bo, JxxPlace.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlaceId(add.getPlaceId());
        }
        return flag;
    }

    /**
     * 修改场所信息
     */
    @Override
    public Boolean updateByBo(JxxPlaceBo bo) {
        JxxPlace update = BeanUtil.toBean(bo, JxxPlace.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JxxPlace entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除场所信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
